package cn.itcast.web.controller.system;

import cn.itcast.domain.system.Module;
import cn.itcast.domain.system.Role;
import cn.itcast.service.system.ModuleService;
import cn.itcast.service.system.RoleService;
import cn.itcast.web.controller.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/role")
public class RoleController extends BaseController {

 @Autowired
 private RoleService roleService;

 @Autowired
 private ModuleService moduleService;

 /**
  * 角色分页
  * /system/role/list.do
  */
 @RequestMapping("/list")
 public String list(
  @RequestParam(defaultValue = "1") int page,
  @RequestParam(defaultValue = "10") int size
 ) {
  //调用service查询分页
  String companyId = getLoginCompanyId();
  PageInfo info = roleService.findAll(companyId,page,size);
  //存入request域
  request.setAttribute("page", info);
  //跳转页面
  return "system/role/role-list";
 }

 /**
  * 跳转保存页面
  */
 @RequestMapping("/toAdd")
 public String toAdd() {
  return "system/role/role-add";
 }

 /**
  * 添加或者更新角色
  */
 @RequestMapping("/edit")
 public String edit(Role role) {
  //设置企业参数
  role.setCompanyId(getLoginCompanyId());
  role.setCompanyName(getLoginCompanyName());
  //判断是否存在id
  if (StringUtils.isEmpty(role.getId())) {
   //id不存在，调用保存
   roleService.save(role);
  } else {
   //id存在，调用更新
   roleService.update(role);
  }
  //跳转页面
  return "redirect:/system/role/list.do";
 }

 /**
  * 跳转更新页面
  * /system/role/toUpdate.do
  */
 @RequestMapping("/toUpdate")
 public String toUpdate(String id) {
  //根据id查询角色
  Role role = roleService.findById(id);
  //存入request域
  request.setAttribute("role",role);
  //跳转页面
  return "system/role/role-update";
 }

 /**
  * 根据id删除
  */
 @RequestMapping("/delete")
 public String delete(String id) {
  roleService.delete(id);
  return "redirect:/system/role/list.do";
 }

 /**
  * 进入角色分配权限的页面
  * /system/role/roleModule.do
  */
 @RequestMapping("/roleModule")
 public String roleModule(String roleid) {
  //根据id查询角色
  Role role = roleService.findById(roleid);
  //存入request域
  request.setAttribute("role",role);
  return "/system/role/role-module";
 }

 /**
  * 处理ajax请求
  */
 @RequestMapping("/getZtreeNodes")
 public @ResponseBody List<Map> getZtreeNodes(String roleId) {
  List<Map> list = new ArrayList<>();
  //查询所有模块
  List<Module> modules = moduleService.findAll();
  //根据id。查询此角色具有的所有模块id
  List<String> moduleIds = roleService.findModulesByRoleId(roleId);
  //循环所有模块数据
  for (Module module : modules) {
   //一个module转化成一个map集合构造节点数据
   Map map = new HashMap();
   map.put("id", module.getId());
   map.put("pId", module.getParentId());
   map.put("name", module.getName());
   if (moduleIds.contains(module.getId())) {
    map.put("checked", true);
   }
   list.add(map);
  }
  return list;
 }

 /**
  * 实现对角色分配权限
  * /system/role/updateRoleModule.do
  */
 @RequestMapping("/updateRoleModule")
 public String updateRoleModule(String roleid, String moduleIds) {
  //调用service
  roleService.updateRoleModule(roleid, moduleIds);
  //重定向
  return "redirect:/system/role/list.do";
 }
}
